﻿/// <reference name="MicrosoftAjax.js" />

// Register the namespace for the control.
var MedianamikLockManagement = null;
Type.registerNamespace('Medianamik');

Medianamik.LockActivator = function Medianamik$LockActivator(element) {
    Medianamik.LockActivator.initializeBase(this, [element]);
    
    this._objectId = null;
    this._lockStopped = false;
    this._lockTimer = '';
    this._period = null;
    this._ultimatumReachedHandler = function() { };

    this._unlockHandler = Function.createDelegate(this, function(e) { this.callUnlockService(e) });

    this._unlockHook = Function.createDelegate(this,
        function() {
            $("a.unlock").bind('click', this._unlockHandler);
            Sys.Application.remove_load(this._unlockHook);
        });

    this._suspendHandler = Function.createDelegate(this, function() {
        Sys.Debug.trace("_suspendHandler executed");
        this.set_lockStopped(true);
    });
    this._suspendHook = Function.createDelegate(this,
       function() {
           $(".SuspendLock").bind('click', this._suspendHandler);
           Sys.Application.remove_load(this._suspendHook);
       });

    MedianamikLockManagement = this;
}

function Medianamik$LockActivator$initialize() {
    Medianamik.LockActivator.callBaseMethod(this, 'initialize');

    Sys.Debug.trace("Medianamik$LockActivator$initialize");
    //Déverrouillage en cas de click sur les liens avec la classe Unlock
    Sys.Application.add_load(this._unlockHook);
    Sys.Application.add_load(this._suspendHook);   

//    var prm = Sys.WebForms.PageRequestManager.getInstance();
//    if (prm.get_isInAsyncPostBack())
//        return;
    this.keepLocked();
}

function Medianamik$LockActivator$dispose() {
    Sys.Debug.trace("Medianamik$LockActivator$dispose");
    $clearHandlers(this.get_element());

    $("a.unlock").unbind('click', this._unlockHandler);
    //$(".SuspendLock").unbind('click', this._suspendHandler);
    this.set_lockStopped(true);
    
    Medianamik.LockActivator.callBaseMethod(this, 'dispose');
}

function Medianamik$LockActivator$keepLocked() {
        // Do not call if current page waiting for next page response (Get or Post) 
        Medianamik.UI.Services.LockWebService.Ping(this.get_objectId(), Function.createDelegate(this, this.pingSucceeded));
}

function Medianamik$LockActivator$callUnlockService(e) {
        this.set_lockStopped(true);
        Medianamik.UI.Services.LockWebService.Unlock(this.get_objectId(), function() { window.location.href = e.target.href; }, function() { window.location.href = e.target.href; });
        return false;
    }

function Medianamik$LockActivator$pingSucceeded(result) {
    if (result == 1) {
        this.set_lockTimer(window.setTimeout(Function.createDelegate(this, this.keepLocked), this.get_period()));
    }
    else {
        this.set_lockStopped(true);
        window.setTimeout(Function.createDelegate(this, this.get_ultimatumReachedHandler()), 500);
    }
}

function Medianamik$LockActivator$get_objectId() {
    return this._objectId;
    }

function Medianamik$LockActivator$set_objectId(value) {
        if (this._objectId !== value) {
            this._objectId = value;
            this.raisePropertyChanged('objectId');
        }
    }

function Medianamik$LockActivator$get_period() {
        return this._period;
    }

function Medianamik$LockActivator$set_period(value) {
        this._period = value;
    }

function Medianamik$LockActivator$get_lockStopped() {
        return this._lockStopped;
    }

function Medianamik$LockActivator$set_lockStopped(value) {
        this._lockStopped = value;
        if (value === true) {
            window.clearTimeout(this._lockTimer);
        }
    }

function Medianamik$LockActivator$get_lockTimer() {
        return this._lockTimer;
    }

function Medianamik$LockActivator$set_lockTimer(value) {
        this._lockTimer = value;
    }

function Medianamik$LockActivator$get_ultimatumReachedHandler() {
        return this._ultimatumReachedHandler;
    }

function Medianamik$LockActivator$set_ultimatumReachedHandler(handler) {
        this._ultimatumReachedHandler = handler;
    }

Medianamik.LockActivator.prototype = {
    initialize: Medianamik$LockActivator$initialize,
    dispose: Medianamik$LockActivator$dispose,
    keepLocked: Medianamik$LockActivator$keepLocked,
    callUnlockService: Medianamik$LockActivator$callUnlockService,
    pingSucceeded: Medianamik$LockActivator$pingSucceeded,
    get_objectId: Medianamik$LockActivator$get_objectId,
    set_objectId: Medianamik$LockActivator$set_objectId,
    get_period: Medianamik$LockActivator$get_period,
    set_period: Medianamik$LockActivator$set_period,
    get_lockStopped: Medianamik$LockActivator$get_lockStopped,
    set_lockStopped: Medianamik$LockActivator$set_lockStopped,
    get_lockTimer: Medianamik$LockActivator$get_lockTimer,
    set_lockTimer: Medianamik$LockActivator$set_lockTimer,
    get_ultimatumReachedHandler: Medianamik$LockActivator$get_ultimatumReachedHandler,
    set_ultimatumReachedHandler: Medianamik$LockActivator$set_ultimatumReachedHandler
}

// Register the class as a type that inherits from Sys.UI.Control.
Medianamik.LockActivator.registerClass('Medianamik.LockActivator', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();







